package com.administration.controller;


import com.administration.constants.Result;
import com.administration.dao.SysUserMapper;
import com.administration.pojo.SysUser;
import com.administration.utils.PasswordUtils;
import com.administration.utils.ResultData;
import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.*;

//@RestController 在类头上加注解，组合注解，@RestController = @COntroller + @ResponseBody

@Controller
@RequestMapping("/system")
public class SystemController {

    @Resource
    private SysUserMapper sysUserMapper;

    //登陆界面
    @RequestMapping("/login.do")
    public String login() {
        return "/static/system/system_login.html";
    }

    //主界面
    @RequestMapping("/index.do")
    public String index() {
        return "/static/system/system_layui.html";
    }

    //登录验证
    @ResponseBody
    @RequestMapping("/validateLogon.do")
    public String validateLogon(SysUser sysUser) {
        Map<String, Object> map = new HashMap<>();
        map.put("loginName", sysUser.getLoginName());
        List<SysUser> list = sysUserMapper.selectUserList(map);
        boolean validateLogonRes = false;
        for (SysUser su : list) {
            //密码判断
            validateLogonRes = PasswordUtils.matches(su.getSalt(), sysUser.getPassword(), su.getPassword());

            if (validateLogonRes == true) {
                StringBuffer url = new StringBuffer();
                url.append("http://localhost:8080/static/system/system_layui.html?userId=" + su.getUserId());
                return url.toString();
            } else {
                return "/static/system/system_login.html";
            }
        }
        return "/static/system/system_login.html";
    }


    //MultipartFile:文件类，根据该类获取文件上传信息
    //文件上传
    @ResponseBody
    @RequestMapping("/fileUploadPersonImg.do")
    public ResultData fileUploadPersonImg(MultipartFile file, HttpServletRequest request) {
        //获取request请求中文件信息

        //1.上传文件数据，数据库中一般存储文件路径，最终的文件通常放在文件服务中
        //file文件协议 http网络请求协议
        //存储文件路径 相对路径   绝对路径
        //获取当前项目的绝对位置(路径)
        String path = request.getSession().getServletContext().getRealPath("upload");
        if (file != null && !file.isEmpty()) {
            //获取multipartFile原文件名
            String fileName = file.getOriginalFilename();
            //根据原文件名更改新名称
            String newFileName = UUID.randomUUID().toString().substring(0, 6) + "." + FilenameUtils.getExtension(fileName);
            File fileNew = new File(path, newFileName);
            //判断当前文件路径是否存在
            if (!fileNew.getParentFile().exists()) {
                //创建
                fileNew.getParentFile().mkdirs();
            }

            try {
                //获取新文件名称及相对路径地址
                String imgUrl = "upload/" + newFileName;

                //保存文件
                file.transferTo(fileNew);
                return new ResultData<>(Result.SUCCESS.getCode(), imgUrl);
            } catch (IOException e) {
                //throw new RuntimeException(e);
                return new ResultData<>(Result.FAILED.getCode(), "");
            }
        }
        return new ResultData<>();
    }
}
